#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;
int getMetadata(fstream &f)
{
    string metadeta;
    f >> metadeta;
    metadeta.assign(metadeta, 3, metadeta.length() - 6);
    stringstream ss;
    ss << metadeta;
    int output;
    ss >> output;
    return output;
}
class Class
{
  public:
    string name;   //班级名称
    string t_name; //老师姓名
    int credit;    //学分
    Class(string input_name, int input_credit, string input_tname)
    {
        name = input_name;
        credit = input_credit;
        t_name = input_tname;
    }
};
class Admin
{
  public:
    string id;       //编号
    string name;     //姓名
    string password; //密码
    Admin(string input_name, string input_id, string i_password)
    {
        name = input_name;
        id = input_id;
        password = i_password;
    }
};

class Student
{
  public:
    string id = "#";                         //编号
    string name = "#";                       //姓名
    string password = "#";                   //密码
    string major = "#", grade = "#", sex = "#", classes = "#"; //专业，年级，性别，所选课程班级
    Student(){}
    Student(string iid, string iname, string isex, string imajor, string igrade, string ipswd, string iclasses)
    {
        id = iid;
        name = iname;
        sex = isex;
        major = imajor;
        grade = igrade;
        password = ipswd;
        classes = iclasses;
    }
    void assign(string iid, string iname, string isex, string imajor, string igrade, string ipswd, string iclasses)
    {
        id = iid;
        name = iname;
        sex = isex;
        major = imajor;
        grade = igrade;
        password = ipswd;
        classes = iclasses;
    }
    void readFromLine(string line)
    {
        stringstream ss;
        ss << line;
        string id, name, pswd, classes, sex, major, grade;
        ss >> id >> name >> sex >> major >> grade >> pswd >> classes;
        this->assign(id, name, sex, major, grade, pswd, classes);
    }
    bool pswdChange()
    {
        string pswd;
        cout << "\nInput your old password:";
        cin >> pswd;
        if (pswd != password)
        {
            cout << "Wrong password. Please try again!\n";
            system("pause");
            return false;
        }
        cout << "Input the new password:";
        cin >> pswd;
        password = pswd;
        /*写入文件*/
        int line = findLineById(id, "./data/studentInfo.txt");
        
        cout << "Password changed successfully!\n";
        system("pause");
        return true;
    }
};
class Teacher
{
  public:
    string id;           //编号
    string name;         //姓名
    string password;     //密码
    string sex, classes; //性别，所授课程班级
    Teacher(string iid, string iname, string isex, string ipswd, string iclasses)
    {
        id = iid;
        name = iname;
        sex = isex;
        password = ipswd;
        classes = iclasses;
    }
    void print()
    {
        cout << id << ' ' << name << ' ' << sex << ' ';
        cout << password << ' ' << classes << endl;
    }
    void assign(string iid, string iname, string isex, string ipswd, string iclasses)
    {
        id = iid;
        name = iname;
        sex = isex;
        password = ipswd;
        classes = iclasses;
    }
    void readFromLine(string line)
    {
        stringstream ss;
        ss << line;
        string id, name, sex, pswd, classes;
        ss >> id >> name >> sex >> pswd >> classes;
        this->assign(id, name, sex, pswd, classes);
    }
};

Student findStudentById(string s_id, string in_fileName)
{
    const char *fname = in_fileName.c_str();
    fstream f;
    f.open(fname, ios::in);
    int s_num = getMetadata(f);
    string id, name, pswd, classes, sex, major, grade;
    Student p;
    for (int i = 0; i < s_num; i++)
    {
        f >> id >> name >> sex >> major >> grade >> pswd >> classes;
        if (id == s_id)
        {
            p.assign(id, name, sex, major, grade, pswd, classes);
            return p;
        }
    }
    return p;
}
int findLineById(string in_id, string in_fileName)
{
    const char *fname = in_fileName.c_str();
    fstream f;
    f.open(fname, ios::in);
    int s_num = getMetadata(f);
    string id, name, pswd, classes, sex, major, grade;
    for (int i = 0; i < s_num; i++)
    {
        f >> id >> name >> sex >> major >> grade >> pswd >> classes;
        if (id == in_id)
            return i;
    }
}

bool SaveToFile(string fname)
{

    return true;
}

bool printFile(string in_fname)
{
    cout << "opening " << in_fname << "..." << endl;
    const char *fname = in_fname.c_str();
    fstream f;
    f.open(fname, ios::in);
    if (!f)
    {
        cout << "file not exist! Please try again.\n";
        return false;
    }
    else
        cout << "file opened!\n";
    int metadeta = getMetadata(f);
    string id, name, pswd, classes, sex;
    for (int i = 0; i < metadeta; ++i)
    {
        f >> id >> name >> sex >> pswd >> classes;
        cout << id << name << sex << pswd << classes;
    }
    return true;
}

